package com.wing.productionPlan.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.alibaba.nacos.common.utils.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wing.common.utils.JsonResult;
import com.wing.common.utils.Result;
import com.wing.enterprise.feign.EnterpriseClient;
import com.wing.enterprise.product.model.entity.ProcessInfo;
import com.wing.productionPlan.dao.SimpleWorkOrderProcessDao;
import com.wing.productionPlan.model.entity.SimpleWorkOrderProcess;
import com.wing.productionPlan.model.request.SimpleWorkOrderProcessForm;
import com.wing.productionPlan.model.response.SimpleWorkOrderProcessVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 简版工单工序表-ServiceImpl
 *
 * @author
 */
@Service
@Slf4j
public class SimpleWorkOrderProcessServiceImpl extends ServiceImpl<SimpleWorkOrderProcessDao, SimpleWorkOrderProcess> implements SimpleWorkOrderProcessService {
    @Autowired
    private EnterpriseClient enterpriseClient;
    @Override
    public int add(SimpleWorkOrderProcessForm simpleWorkOrderProcessForm) {

        //校验

        SimpleWorkOrderProcess simpleWorkOrderProcess =new SimpleWorkOrderProcess();
        CopyOptions copyOptions = CopyOptions.create()
        .setEditable(SimpleWorkOrderProcess.class)
        .setIgnoreError(true)
        .setIgnoreNullValue(true);
        BeanUtil.copyProperties(simpleWorkOrderProcessForm, simpleWorkOrderProcess, copyOptions);

        return baseMapper.insert(simpleWorkOrderProcess);
    }

    @Override
    public List<SimpleWorkOrderProcessVO> list4app(Map<String, Object> params) {
        return baseMapper.list4app(params);
    }

    @Override
    public Page<SimpleWorkOrderProcessVO> pageList4app(Page page, Map<String, Object> params) {
        List<SimpleWorkOrderProcessVO> list = baseMapper.list4app(page, params);
        if(CollectionUtils.isEmpty(list)){
            JsonResult<List<ProcessInfo>> allProcessInfo = enterpriseClient.getAllProcessInfo();
                if(allProcessInfo.getCode() == Result.Code.SUCCESS.getCode() && allProcessInfo.getData() != null){
                    for (ProcessInfo item : allProcessInfo.getData()){
                        SimpleWorkOrderProcessVO orderProcessVO = new SimpleWorkOrderProcessVO();
                        orderProcessVO.setProcessId(item.getId());
                        orderProcessVO.setNodeNames(item.getNodeNames());
                        list.add(orderProcessVO);
                    }
                }

        }
        page.setRecords(list);
        return page;
    }

    @Override
    public SimpleWorkOrderProcessVO get4appById(Long id) {
        return baseMapper.get4appById(id);
    }
}
